<template>
	<div class="particles-js-box">
		<div id="particles-js">
			<h2 class="title">{{ $t('message.title') }}</h2>

			<div class="input-container">
				<el-input v-model="state.ruleForm.userName" :placeholder="$t('message.placeholder1')" class="bottom-border-input" />
				<div class="spacing"></div>
				<el-input v-model="state.ruleForm.password" :placeholder="$t('message.placeholder2')" show-password class="bottom-border-input" />
			</div>

			<div class="loginBtn">
				<a href="javascript:;" @click="onSignIn">
					<p style="font-size: 20px; font-weight: bold">{{ $t('message.btnText') }}</p>
					<span></span>
					<span></span>
					<span></span>
					<span></span>
				</a>
			</div>
		</div>
	</div>
</template>

<script setup name="loginAccount">
import particlesJs from './particles.js';
import particlesConfig from './particles.json';
import { reactive, computed, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useI18n } from 'vue-i18n';
import Cookies from 'js-cookie';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '/@/stores/themeConfig';
import { initFrontEndControlRoutes } from '/@/router/frontEnd';
import { initBackEndControlRoutes } from '/@/router/backEnd';
import { Session } from '/@/utils/storage';
import { formatAxis } from '/@/utils/formatTime';
import { NextLoading } from '/@/utils/loading';

// 定义变量内容
const { t } = useI18n();
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
const route = useRoute();
const router = useRouter();
const state = reactive({
	isShowPassword: false,
	ruleForm: {
		userName: 'admin',
		password: '123456',
		code: '1234',
	},
	loading: {
		signIn: false,
	},
});

// 时间获取
const currentTime = computed(() => {
	return formatAxis(new Date());
});

// 登录
const onSignIn = async () => {
	state.loading.signIn = true;
	// 存储 token 到浏览器缓存
	Session.set('token', Math.random().toString(36).substr(0));
	// 模拟数据，对接接口时，记得删除多余代码及对应依赖的引入。用于 `/src/stores/userInfo.ts` 中不同用户登录判断（模拟数据）
	Cookies.set('userName', state.ruleForm.userName);

	if (!themeConfig.value.isRequestRoutes) {
		// 前端控制路由，2、请注意执行顺序
		const isNoPower = await initFrontEndControlRoutes();
		signInSuccess(isNoPower);
	} else {
		// 模拟后端控制路由，isRequestRoutes 为 true，则开启后端控制路由
		// 添加完动态路由，再进行 router 跳转，否则可能报错 No match found for location with path "/"
		const isNoPower = await initBackEndControlRoutes();
		// 执行完 initBackEndControlRoutes，再执行 signInSuccess
		signInSuccess(isNoPower);
	}
};

// 登录成功后的跳转
const signInSuccess = (isNoPower) => {
	if (isNoPower) {
		const promptText = t('message.promptText');
		ElMessage.warning(`${promptText}`);
		Session.clear();
	} else {
		// 初始化登录成功时间问候语
		let currentTimeInfo = currentTime.value;
		// 登录成功，跳到转首页
		// 如果是复制粘贴的路径，非首页/登录页，那么登录成功后重定向到对应的路径中
		if (route.query?.redirect) {
			router.push({
				path: route.query?.redirect,
				query: Object.keys(route.query?.params).length > 0 ? JSON.parse(route.query?.params) : '',
			});
		} else {
			router.push('/smartLargeScreen/mainStationScreenNews');
		}
		// 登录成功提示
		const signInText = t('message.signInText');
		ElMessage.success(`${currentTimeInfo}，${signInText}`);
		// 添加 loading，防止第一次进入界面时出现短暂空白
		// NextLoading.start(); 
	}
	state.loading.signIn = false;
};

onMounted(() => {
	particlesJs('particles-js', particlesConfig);
	document.body.style.overflow = 'hidden';
});
</script>

<style scoped>
.particles-js-box {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	z-index: 1;
	background: url('../../assets/login_background.png') no-repeat center/cover;
}

#particles-js {
	width: 100%;
	height: 100%;
	background-color: transparent; /* 改为透明 */
	position: relative;
}

.title {
	font-size: 40px;
	color: #fff;
	position: fixed;
	top: 30%;
	width: 100%;
	text-align: center;
}

.input-container {
	position: fixed;
	top: 42%;
	left: 36%;
	width: 27.5%;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
}

.spacing {
	height: 30px;
}

.bottom-border-input ::v-deep .el-input__wrapper {
	border: none; /* 移除默认边框 */
	border-radius: 0; /* 移除圆角 */
	box-shadow: none !important; /* 移除默认阴影 */
	border-bottom: 1px solid #dcdfe6; /* 只显示底部边框 */
	background-color: transparent; /* 可选：背景透明 */
}

.bottom-border-input ::v-deep .el-input__wrapper:hover {
	border-bottom-color: #409eff; /* 鼠标悬停时底部边框变色 */
}

.bottom-border-input ::v-deep .el-input__wrapper.is-focus {
	border-bottom-color: #409eff; /* 聚焦时底部边框变色 */
}

.loginBtn {
	position: fixed;
	top: 66%;
	width: 100%;
	text-align: center;
}

.loginBtn a {
	display: inline-block;
	padding: 10px 20px;
	color: #03e9f4;
	text-decoration: none;
	transition: all 0.5s;
	position: relative;
}

.loginBtn a:hover {
	color: #fff;
	border-radius: 5px;
	background-color: #03e9f4;
	box-shadow:
		0 0 5px #03e9f4,
		0 0 25px #03e9f4,
		0 0 50px #03e9f4,
		0 0 100px #03e9f4;
}

.loginBtn a span {
	position: absolute;
}

.loginBtn a span:first-child {
	top: 0;
	left: -100%;
	width: 100%;
	height: 2px;
	background: linear-gradient(to right, transparent, #03e9f4);
	animation: move1 1s linear infinite;
}

.loginBtn a span:nth-child(2) {
	right: 0;
	top: -100%;
	width: 2px;
	height: 100%;
	background: linear-gradient(transparent, #03e6f4);
	animation: move2 1s linear 0.25s infinite;
}

.loginBtn a span:nth-child(3) {
	right: -100%;
	bottom: 0;
	width: 100%;
	height: 2px;
	background: linear-gradient(to left, transparent, #03e9f4);
	animation: move3 1s linear 0.5s infinite;
}

.loginBtn a span:last-child {
	left: 0;
	bottom: -100%;
	width: 2px;
	height: 100%;
	background: linear-gradient(#03e9f4, transparent);
	animation: move4 1s linear 0.75s infinite;
}

@keyframes move1 {
	0% {
		left: -100%;
	}
	50%,
	100% {
		left: 100%;
	}
}

@keyframes move2 {
	0% {
		top: -100%;
	}
	50%,
	100% {
		top: 100%;
	}
}

@keyframes move3 {
	0% {
		right: -100%;
	}
	50%,
	100% {
		right: 100%;
	}
}

@keyframes move4 {
	0% {
		bottom: -100%;
	}
	50%,
	100% {
		bottom: 100%;
	}
}
</style>
